library(tidyverse)
library(cepespR)
library(readxl)
library(data.table)
library(stargazer)
library(haven)

#utilizar file= arquivo1.csv
library(readr)
arquivo1 <- read_delim("arquivo1.csv", 
                       delim = ";", escape_double = FALSE, locale = locale(decimal_mark = ",", 
                                                                           grouping_mark = ".", encoding = "WINDOWS-1252"), 
                       trim_ws = TRUE)



FN_e<-filter(arquivo1,Dep_Federal==0)
FN_f<-filter(arquivo1,Dep_Federal==1)



Posi_federal<-FN_f %>% count(Posicao, sort=TRUE)%>% 
  mutate(percent = n/sum(n)*100) %>%
  mutate(cum = cumsum(percent)/sum(percent)*100)%>%
  arrange(Posicao)
Posi_federal<-round(Posi_federal, digits = 1)
colSums(Posi_federal[c(6:45),2:3 ], na.rm=TRUE)
colSums(Posi_federal[c(1:45),2:3 ], na.rm=TRUE)
Posicao<-c("restante")
n<-c(173)
percent<-(5.4)
cum<-(100)
restante <- data.frame(Posicao,n,percent,cum)

Posicao<-c("Total")
n<-c(3144)
percent<-(100)
cum<-("-")

Total <- data.frame(Posicao,n,percent,cum)

Posi_federal<-Posi_federal[1:5,1:4]
Posi_federal<-rbind(Posi_federal,restante,Total)


Posi_estadual<-FN_e %>% count(Posicao, sort=TRUE)%>% 
  mutate(percent = n/sum(n)*100) %>%
  mutate(cum = cumsum(percent)/sum(percent)*100)%>%
  arrange(Posicao)
Posi_estadual<-round(Posi_estadual, digits = 1)
colSums(Posi_estadual[c(6:52),2:3 ], na.rm=TRUE)
colSums(Posi_estadual[c(1:52),2:3 ], na.rm=TRUE)
Posicao<-c("restante")
n<-c(392)
percent<-(5.1)
cum<-(100)
restante <- data.frame(Posicao,n,percent,cum)

Posicao<-c("Total")
n<-c(7608)
percent<-(100)
cum<-("-")

Total <- data.frame(Posicao,n,percent,cum)


Posi_estadual<-Posi_estadual[1:5,1:4]
Posi_estadual<-rbind(Posi_estadual,restante,Total)

FN_f%>%filter(Posicao==1)->P1
FN_f%>%filter(Posicao==2)->P2
FN_f%>%filter(Posicao==3)->P3
FN_f%>%filter(Posicao==4)->P4
FN_f%>%filter(Posicao==5)->P5
FN_f%>%filter(Posicao>5)->P_5

a<-summary(P1$V_Partic)
b<-summary(P2$V_Partic)
c<-summary(P3$V_Partic)
d<-summary(P4$V_Partic)
e<-summary(P5$V_Partic)
f<-summary(P_5$V_Partic)
g<-summary(FN_f$V_Partic)

Part_federal<-rbind(a,b,c,d,e,f,g)
Part_federal<-round(Part_federal, digits = 2)
Part_federal<-as_tibble(Part_federal)
Part_federal<-select(Part_federal,"1st Qu.":"3rd Qu.")

Tabela3_federal<-cbind(Posi_federal,Part_federal)

FN_e%>%filter(Posicao==1)->P1
FN_e%>%filter(Posicao==2)->P2
FN_e%>%filter(Posicao==3)->P3
FN_e%>%filter(Posicao==4)->P4
FN_e%>%filter(Posicao==5)->P5
FN_e%>%filter(Posicao>5)->P_5

a<-summary(P1$V_Partic)
b<-summary(P2$V_Partic)
c<-summary(P3$V_Partic)
d<-summary(P4$V_Partic)
e<-summary(P5$V_Partic)
f<-summary(P_5$V_Partic)
g<-summary(FN_e$V_Partic)

Part_estadual<-rbind(a,b,c,d,e,f,g)
Part_estadual<-round(Part_estadual, digits = 2)
Part_estadual<-as_tibble(Part_estadual)
Part_estadual<-select(Part_estadual,"1st Qu.":"3rd Qu.")

Tabela3_estadual<-cbind(Posi_estadual,Part_estadual)

rm(P_5,P1,P2,P3,P4,P5,Part_estadual,Part_federal,Posi_estadual,Posi_federal,
   restante,Total,a,b,c,cum,d,e,f,g,n,percent,Posicao)



